package com.haiyou.dc.es.bi.impl;

import com.haiyou.common.constant.MsgCode;
import com.haiyou.common.exception.BusinessException;
import com.haiyou.dc.es.constant.EsConstant;
import com.haiyou.dc.es.entity.EsHouse;
import com.haiyou.dc.es.entity.EsHouseDetail;
import com.haiyou.dc.es.entity.EsHouseTag;
import com.haiyou.dc.es.entity.EsSupportAddress;
import com.haiyou.dc.es.entity.es.HouseIndexTemplate;
import com.haiyou.dc.es.service.EsHouseDetailService;
import com.haiyou.dc.es.service.EsHouseService;
import com.haiyou.dc.es.service.EsHouseTagService;
import com.haiyou.dc.es.service.EsSupportAddressService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author pc
 */
@Service
@Slf4j
public class EsXunWuHouseService extends EsSearchServiceImpl {

    @Autowired
    private EsHouseService esHouseService;
    @Autowired
    private EsHouseDetailService esHouseDetailService;
    @Autowired
    private EsSupportAddressService esSupportAddressService;
    @Autowired
    private EsHouseTagService esHouseTagService;

    @Override
    protected String getIndexName() {
        return EsConstant.XUNWU_INDEX_NAME;
    }

    @Override
    protected String getTypeName() {
        return EsConstant.XUNWU_TYPE_NAME;
    }

    public void addHouse(Integer id) {
        EsHouse esHouse = esHouseService.selectByPrimaryKey(id);
        if (esHouse == null) {
            throw new BusinessException(MsgCode.NOT_FOUNT);
        }
        HouseIndexTemplate houseIndexTemplate = new HouseIndexTemplate();
        houseIndexTemplate.setHouseId(Long.valueOf(id));
        BeanUtils.copyProperties(esHouse, houseIndexTemplate);

        EsHouseDetail esHouseDetail = esHouseDetailService.selectOne(new EsHouseDetail().setHouseId(id));
        if (esHouseDetail != null) {
            BeanUtils.copyProperties(esHouseDetail, houseIndexTemplate);
        }

        List<EsHouseTag> esHouseTagList = esHouseTagService.select(new EsHouseTag().setHouseId(id));
        if (esHouseTagList != null && esHouseTagList.size() > 0) {
            List<String> tags = esHouseTagList.stream().map(e -> e.getName()).collect(Collectors.toList());
            houseIndexTemplate.setTags(tags);
        }

        EsSupportAddress esCity = esSupportAddressService.getAddress(esHouse.getCityEnName(), EsConstant.Level.CITY.getValue());
        EsSupportAddress esRegion = esSupportAddressService.getAddress(esHouse.getRegionEnName(), EsConstant.Level.REGION.getValue());
        String address = esCity.getCnName() + esRegion.getCnName() + esHouse.getStreet() + esHouse.getDistrict() + esHouseDetail.getAddress();
        //TODO 地理位置
        log.info("---------------------------->houseIndexTemplate={}", houseIndexTemplate);
        super.create(String.valueOf(houseIndexTemplate.getHouseId()), houseIndexTemplate);
    }


}
